/**
 * 安卓白金英雄坛制作组 <br>
 * 文件名：EditNicknameDialog.java <br>
 * 创建时间：2013-5-21 下午4:07:07 <br>
 * 所属项目：GmudEX <br>
 * @author 12548 <br>
 */
package lostland.gmud.exv2.ui.meta;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import cn.bmob.v3.listener.UpdateListener;
import lostland.gmud.exv2.Game;
import lostland.gmud.exv2.R;
import lostland.gmud.exv2.community.GmudUser;
import lostland.gmud.exv2.ui.LockedScreen;

/**
 * 类名：EditNicknameDialog <br>
 * 说明：修改昵称
 *
 * @author 12548
 */
public class EditNicknameDialog extends Dialog {

	private Window mWindow;

	EditText et;


	/**
	 * @param context
	 */
	public EditNicknameDialog(Context context) {
		super(context);
		mWindow = this.getWindow();
		mWindow.setBackgroundDrawable(new ColorDrawable(0));
		mWindow.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		WindowManager.LayoutParams lp = mWindow.getAttributes();
		lp.gravity = Gravity.CENTER;
		lp.dimAmount = 0.0001f;
		mWindow.setAttributes(lp);
		setCanceledOnTouchOutside(true);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		mWindow.setContentView(R.layout.dialog_changenickname);

		et = (EditText) findViewById(R.id.etNickname);
		Button btn = (Button) findViewById(R.id.btnEditNickname);
		btn.setOnClickListener(new View.OnClickListener() {

			                       @Override
			                       public void onClick(View v) {

				                       String x = et.getText().toString().trim();
				                       if (x.length() < 2) {
					                       Toast.makeText(Game.getInstance(), "昵称必须在2个字符以上！", Toast.LENGTH_SHORT).show();
					                       return;
				                       }

				                       GmudUser me = GmudUser.getInstance();

				                       me.setNickName(x);
				                       new LockedScreen("正在更新数据").pushToGame();
				                       me.update(Game.getInstance(), new UpdateListener() {

					                       @Override
					                       public void onFailure(int arg0, String arg1) {
						                       Toast.makeText(Game.getInstance(), "更新数据失败" + arg1, Toast.LENGTH_SHORT).show();
						                       Game.getInstance().popScreen();
						                       EditNicknameDialog.this.cancel();
						                       return;
					                       }

					                       @Override
					                       public void onSuccess() {
						                       Toast.makeText(Game.getInstance(), "昵称修改成功！", Toast.LENGTH_SHORT).show();
						                       Game.getInstance().popScreen();
						                       EditNicknameDialog.this.cancel();
						                       return;
					                       }
				                       });


			                       }
		                       }
		);
	}


}
